package com.ztjava.core;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.ztjava.core.interceptor.CoreInterceptor;
import com.ztjava.core.log.Log;
import com.ztjava.core.util.FileUtil;

public class HSys extends JFinalConfig {
	private final static Log log = Log.getLog(HSys.class);
	private static JSONObject config;
	
	static {
		config = JSONObject.parseObject(FileUtil.readFileToString("hsys.json"));
		//初始化各个插件
		JSONArray plugins = config.getJSONArray("plugin");
		for(int i=0;i<plugins.size();i++){
			JSONObject pluginObject = plugins.getJSONObject(i);
			try{
				@SuppressWarnings("unchecked")
				Class<HPlugin> plugin = (Class<HPlugin>)Class.forName(pluginObject.getString("package")+"."+pluginObject.getString("class"));
				if(pluginObject.get(pluginObject.getString("class"))!=null){
					log.warn(pluginObject.getString("class")+"插件重复加载！");
				}
				Constant.pluginObject.put(pluginObject.getString("class"), plugin.newInstance());
				Constant.pluginPackage.put(pluginObject.getString("class"), pluginObject.getString("package"));
				log.info(pluginObject.getString("class")+"插件加载成功！");
			}catch (Exception e) {
				log.error(pluginObject.getString("class")+"插件加载失败！");
				throw new RuntimeException(pluginObject.getString("class")+"插件加载失败！");
			}
		}
	}
	
	@Override
	public void configConstant(Constants me) {
		log.info("configConstant");
		if(config.get("devMode")!=null) me.setDevMode(config.getBoolean("devMode"));
		for(String key:Constant.pluginObject.keySet()){
			Constant.pluginObject.get(key).configConstant(me);
		}
	}

	@Override
	public void configRoute(Routes me) {
		log.info("configRoute");
		
		for(String key:Constant.pluginObject.keySet()){
			Constant.pluginObject.get(key).configRoute(me);
		}
	}

	@Override
	public void configPlugin(Plugins me) {
		log.info("configPlugin");
		JSONObject database = config.getJSONObject("database");
		if(database!=null){
			log.info("开始连接数据库："+database.getString("dbname"));
			Constant.dbName = database.getString("dbname");
			if(database.get("updateTable")!=null) Constant.updateTable = database.getBoolean("updateTable");
			C3p0Plugin c3p0Plugin = new C3p0Plugin("jdbc:mysql://"+database.getString("host")
					+":"+database.getString("port")+"/"
					+database.getString("dbname")+"?useUnicode=true&characterEncoding=utf-8",
					database.getString("username"),database.getString("password"));
			me.add(c3p0Plugin);
			
			ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
			if(database.get("showSql")!=null) arp.setShowSql(database.getBoolean("showSql"));
			me.add(arp);
			
			//初始化ARP插件的各个插件的Model
			for(String key:Constant.pluginObject.keySet()){
				Constant.pluginObject.get(key).configArp(arp);
			}
			
			c3p0Plugin.start();
			TableBulider.bulderTable(c3p0Plugin.getDataSource());
		}
		me.add(new EhCachePlugin());
	}

	@Override
	public void configInterceptor(Interceptors me) {
		log.info("configInterceptor");
		me.addGlobalActionInterceptor(new CoreInterceptor());
		for(String key:Constant.pluginObject.keySet()){
			Constant.pluginObject.get(key).configInterceptor(me);
		}
	}

	@Override
	public void configHandler(Handlers me) {
		log.info("configHandler");
		for(String key:Constant.pluginObject.keySet()){
			Constant.pluginObject.get(key).configHandler(me);
		}
	}
	public void afterJFinalStart(){
		log.info("afterJFinalStart");
		InitConfig.setParam("项目路径", "PATH", FileUtil.getAppPath(), "不要以/结尾");
		//必须分开，以便覆盖配置
		for(String key:Constant.pluginObject.keySet()){
			InitConfig.loadPluginConfig(Constant.pluginPackage.get(key),key);
		}
		//可进行项目级别自定义参数，覆盖各插件参数
		InitConfig.loadAllConfig("config.json");
		for(String key:Constant.pluginObject.keySet()){
			Constant.pluginObject.get(key).afterJFinalStart();
		}
	}
}
